define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    $('.rechargetype').on('change',function () {
        $('#recharge_text').html($(this).find("option:selected").text());
    })

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/wallet/index',
                    add_url: 'user/wallet/add',
                    edit_url: 'user/wallet/edit',
                    //del_url: 'user/wallet/del',
                    multi_url: 'user/wallet/multi',
                    table: 'wallet',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'user.username', title: '会员账号',operate:'IN'},
                        {field: 'user.group_id', title: '角色', operate:false,searchList: {"1":'系统账号',"2":'玩家',"3":'代理'}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'name', title: '姓名'},
                        {
                            field: 'back_tag_name',
                            title: '开户行',
                            operate: false,
                            searchList: {
                                "ICBC":'中国工商银行',"CMBC":'招商银行',"ABC":'中国农业银行',"CBC":'中国建设银行',
                                "BC":'中国银行',"CMSB":'中国民生银行',"CEB":'中国光大银行',"CITIC":'中信银行',
                                "CTB":'交通银行',"CIB":'兴业银行',"SPDB":'上海浦东发展银行',"PBC":'中国人民银行',
                                "HXB":'华夏银行',"PSBC":'中国邮政储蓄银行'
                            }
                        },

                        {field: 'idcard', title: '银行卡号'},
                        {field: 'createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'up_score',
                                    title: '上分',
                                    text:'上分',
                                    classname: 'btn btn-xs btn-success up-score',
                                },
                                {
                                    name: 'down_score',
                                    title: '下分',
                                    text:'下分',
                                    classname: 'btn btn-xs btn-danger down-score',
                                },
                                {
                                    name: 'give_score',
                                    title: '赠送',
                                    text:'赠送',
                                    classname: 'btn btn-xs btn-danger give-score',
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                visible: false,
                searchFormVisible:true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".up-score", function () { //上分

                var index = $(this).parent().parent().attr('data-index');
                var data = Table.api.getrowdata(table, index);
                var url = 'user/wallet/upScore';
                layer.open({
                    title:['上分('+ data.group_name +':'+ data.user.username +')','font-size:20px;color:red;'],
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: $('#up-score'),
                    btn:['确认','取消'],
                    yes: function(index, layero){
                        layer.load(2);
                        var money = $('#recharge_money').val();
                        var remarks = $('#recharge_text').val();
                        var type = $('.rechargetype').val();
                        $.post(
                            url,
                            {id:data.id,money:money,remarks:remarks,type:type},
                            function (data) {
                                if( data.code == 0 ){
                                    Toastr.error(data.msg);
                                }else{
                                    Toastr.success(data.msg);
                                    Layer.closeAll()
                                    $('#recharge_text').html('微信充值');
                                    $('#recharge_money').val(0);
                                    $(".rechargetype option:first").prop("selected", 'selected');
                                    table.bootstrapTable('refresh');
                                }
                            }
                        );
                    }

                });

            });
            $(document).on("click", ".down-score", function () { //下分

                var index = $(this).parent().parent().attr('data-index');
                var data = Table.api.getrowdata(table, index);
                var url = 'user/wallet/downScore';
                layer.open({
                    title:['下分('+ data.group_name +':'+ data.user.username +')','font-size:20px;color:red;'],
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: $('#down-score'),
                    btn:['确认','取消'],
                    yes: function(index, layero){
                        var money = $('#down_money').val();
                        var remarks = $('#down_text').val();
                        layer.load(2);
                        $.post(
                            url,
                            {id:data.id,money:money,remarks:remarks},
                            function (data) {
                                if( data.code == 0 ){
                                    Toastr.error(data.msg);
                                }else{
                                    Toastr.success(data.msg);
                                    Layer.closeAll();
                                    $('#down_text').html('扣除');
                                    $('#down_money').val(0);
                                    table.bootstrapTable('refresh');
                                }
                            }
                        );
                    }

                });

            });
            $(document).on("click", ".give-score", function () { //赠送
                var index = $(this).parent().parent().attr('data-index');
                var data = Table.api.getrowdata(table, index);
                var url = 'user/wallet/giveScore';
                layer.open({
                    title:['赠送('+ data.group_name +':'+ data.user.username +')','font-size:20px;color:red;'],
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: $('#give'),
                    btn:['确认','取消'],
                    yes: function(index, layero){
                        var money = $('#moneyY').val();
                        var remarks = $('#text').val();
                        var multiples = $('#multiples').val() ? $('#multiples').val() : 1 ;
                        layer.load(2);
                        $.post(
                            url,
                            {id:data.id,money:money,remarks:remarks,multiples:multiples},
                            function (data) {
                                if( data.code == 0 ){
                                    Toastr.error(data.msg);
                                }else{
                                    Toastr.success(data.msg);
                                    Layer.closeAll();
                                    table.bootstrapTable('refresh');
                                }
                            }
                        );
                    }

                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail:function () {
            Controller.api.aaa();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            aaa:function () {
                console.log(111);
            }
        }

    };
    return Controller;
});